package cn.iocoder.yudao.module.fengdong.smartelderly.service.commodity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.commodity.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.commodity.CommodityDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.commodity.CommodityConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.commodity.CommodityMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 商品 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class CommodityServiceImpl implements CommodityService {

    @Resource
    private CommodityMapper commodityMapper;

    @Override
    public Long createCommodity(CommodityCreateReqVO createReqVO) {
        // 插入
        CommodityDO commodity = CommodityConvert.INSTANCE.convert(createReqVO);
        commodityMapper.insert(commodity);
        // 返回
        return commodity.getId();
    }

    @Override
    public void updateCommodity(CommodityUpdateReqVO updateReqVO) {
        // 校验存在
        validateCommodityExists(updateReqVO.getId());
        // 更新
        CommodityDO updateObj = CommodityConvert.INSTANCE.convert(updateReqVO);
        commodityMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommodity(Long id) {
        // 校验存在
        validateCommodityExists(id);
        // 删除
        commodityMapper.deleteById(id);
    }

    private void validateCommodityExists(Long id) {
        if (commodityMapper.selectById(id) == null) {
            throw exception(COMMODITY_NOT_EXISTS);
        }
    }

    @Override
    public CommodityDO getCommodity(Long id) {
        return commodityMapper.selectById(id);
    }

    @Override
    public List<CommodityDO> getCommodityList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return commodityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CommodityDO> getCommodityPage(CommodityPageReqVO pageReqVO) {
        return commodityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CommodityDO> getCommodityList(CommodityExportReqVO exportReqVO) {
        return commodityMapper.selectList(exportReqVO);
    }

    @Override
    public IPage<CommodityRespIpageVO> getCommodityIPage(CommodityIPageReqVO pageVO) {
        pageVO.setUserId(getLoginUserId());
        return commodityMapper.getCommodityIPage(new Page(pageVO.getPageNo(), pageVO.getPageSize()), pageVO);
    }

}
